<%--
  Created by IntelliJ IDEA.
  User: czq
  Date: 2018/5/16
  Time: 11:57
  To change this template use File | Settings | File Templates.
--%>
<%@page pageEncoding="UTF-8" language="java" %>
<%@ include file="/commons/global.jsp"%>
<html>
<head>
    <title>添加院系</title>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/html5shiv.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/static/lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/static/h-ui.admin/css/style.css" />
    <!--[if IE 6]>-->
    <script type="text/javascript" src="http://lib.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
</head>
<body>
    <article class="cl pd-20">
        <form action="<%=request.getContextPath()%>/manage/depart/add" id="pageForm" method="post" class="form form-horizontal" id="form-depart-add">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>院系名称：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="院系名称" id="name" name="name">
                    <input name="name2" class="input-text" id="name2" value= "院系名称已经存在" readonly style="color:#F00;" hidden />
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>院系编号：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" autocomplete="off" value="" placeholder="院系编号" id="code" name="code">
                    <input name="code2" class="input-text" id="code2" value= "院系编号已经存在" readonly style="color:#F00;" hidden />
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>院系简称：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="院系简称" id="abbr" name="abbr">
                    <input name="abbr2" class="input-text" id="abbr2" value= "院系简称已经存在" readonly style="color:#F00;" hidden />
                </div>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <input class="btn btn-primary radius" type="button" id="buttonSubmit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                    <button id="cancel" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </form>
    </article>
</body>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/h-ui/js/H-ui.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/h-ui.admin/js/H-ui.admin.page.js"></script>
<!--/_footer /作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
    function jumpUrl(){
        parent.window.location.reload();
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    }

    $("#cancel").click(function(){
        parent.layer.close(parent.index);
    });
    $("#name").blur(function(){
        var name = $("#name").val();
        //用ajax去数据库匹配，
        $.ajax({
            url:"<%=request.getContextPath()%>/manage/depart/validate",
            async : false,
            type : "get",
            contentType:"application/json;charset=utf-8",
            data : { "name" : name, "code":null, "abbr" : null},
            success : function(data) {
                if (data.msg != "") {
                    $("#name2").show();
                    $("#name").val("");
                } else {
                    $("#name2").css('display','none');
                }
            }
        });
    });


    $("#code").blur(function(){
        var code = $("#code").val();
        //用ajax去数据库匹配，
        $.ajax({
            url:"<%=request.getContextPath()%>/manage/depart/validate",
            async : false,
            type : "get",
            contentType:"application/json;charset=utf-8",
            data : { "name" : null, "code":code, "abbr" : null},
            success : function(data) {
                if (data.msg != "") {
                    $("#code2").show();
                    $("#code").val("");
                } else {
                    $("#code2").css('display','none');
                }
            }
        });
    });

    $("#abbr").blur(function(){
        var abbr = $("#abbr").val();
        //用ajax去数据库匹配，
        $.ajax({
            url:"<%=request.getContextPath()%>/manage/depart/validate",
            async : false,
            type : "get",
            contentType:"application/json;charset=utf-8",
            data : { "name" : null, "code":null, "abbr" : abbr},
            success : function(data) {
                if (data.msg != "") {
                    $("#abbr2").show();
                    $("#abbr").val("");
                } else {
                    $("#abbr2").css('display','none');
                }
            }
        });
    });


    function departAdd(){
        var name = $("#name").val();
        var code = $("#code").val();
        var abbr = $("#abbr").val();
        $.ajax({
            url: $("#pageForm").attr("action"),
            data: {"name" : name, "code":code ,"abbr":abbr},
            type: "post",
            success: function(result){
                if(result!=0){
                    top.layer.open({
                        icon: 1,
                        content: '保存成功！',
                        time: 1500
                    });
                    $(window.parent.document).find("#pageForm").submit();
                    jumpUrl();
                }else{
                    top.layer.open({
                        icon: 2,
                        content: '保存失败！',
                        time: 1500
                    });
                }
            }
        });
    }
    $(function(){

        /* 校验 */
        var departForm;

        $("#buttonSubmit").click(function(){
            if(!departForm.form()){
                return false;
            }else{
                departAdd();
            }
        });

        departForm = $("#pageForm").validate({
            "rules":{
                "name":{
                    "required":true,
                    remote:
                        $.ajax({
                        url:"<%=request.getContextPath()%>/manage/depart/validate",
                        async : false,
                        type : "get",
                        contentType:"application/json;charset=utf-8",
                        data : { "name" : name, "code":null, "abbr" : null},
                        success:function (result) {
                            if(result != 0){
                                return true;
                            }
                            return false;
                        }
                    })
                },
                "code":{
                    "required":true
                },
                "abbr":{
                    "required":true
                }
            },
            "messages":{
                "name":{
                    "required":"院系名称不能为空！",
                    "remote":"院系名称已经存在!"
                },
                "code":{
                    "required":"院系编号不能为空！"
                },
                "abbr":{
                    "required":"院系简称不能为空!"
                }
            }
        });
    });
</script>
</html>
